﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

namespace ReconUI
{
    public partial class RoleMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((UserVO)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }

            if (!IsPostBack)
            {
                bntbrowse.Enabled = false;
                bntgetdetails.Enabled = false;
                rbOption.Focus();
            }
        }

        protected void bntbrowse_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<SCRIPT>window.open('SearchRoleCode.aspx?textbox=" + txtRoleCode.Value + "','_Parent','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
            txtRoleCode.Disabled = false;
        }

        protected void bntgetdetails_Click(object sender, EventArgs e)
        {
            DataTable dtUserDetails = new DataTable();
            DBFactory objDBfactory = new DBFactory();

            try
            {
                txtRoleCode.Disabled = false;
                if (txtRoleCode.Value.Trim() != "")
                {
                    dtUserDetails = objDBfactory.GetRoleDetailsByRoleId(Convert.ToString(txtRoleCode.Value.Trim()), 1);

                    if ((dtUserDetails) == null)
                    {
                        btnClickPopup.Show();
                        lblMessage.Text = "No record found for this role!";
                    }
                    else if (dtUserDetails.Rows.Count == 1)
                    {
                        ResetFields();
                        //Role_Id,Role_Code,Role_Desc,Role_Type,Is_Enabled,Access_Rights                      
                        txtRoleCode.Value = dtUserDetails.Rows[0].ItemArray[1].ToString();
                        txtRoleDesc.Text = dtUserDetails.Rows[0].ItemArray[2].ToString();
                        ddlRoleType.ClearSelection();
                        ddlRoleType.Items.FindByValue(dtUserDetails.Rows[0].ItemArray[3].ToString()).Selected = true;

                        if (dtUserDetails.Rows[0].ItemArray[4].ToString() == "True")
                            chkEnable.Checked = true;
                        else
                            chkEnable.Checked = false;

                        rbOption.SelectedValue = "2";
                        txtRoleCode.Disabled = true;
                    }
                    else
                    {
                        btnClickPopup.Show();
                        lblMessage.Text = "No record found for this role!";
                    }
                }
                else
                {
                    btnClickPopup.Show();
                    lblMessage.Text = "Enter Role Code!";

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if ((dtUserDetails) != null)
                {
                    dtUserDetails.Dispose();
                }
                dtUserDetails = null;

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string resp = string.Empty;
            DBFactory db = new DBFactory();
            DataTable dtUserDetails = new DataTable();
            int is_Enabled = 0;            
           
            if (chkEnable.Checked)
                is_Enabled = 1;            

            if (rbOption.SelectedValue == "1")
            {

                dtUserDetails = db.GetRoleDetailsByRoleId(Convert.ToString(txtRoleCode.Value.Trim()), 1);

                if (dtUserDetails.Rows.Count == 1)
                {
                    btnClickPopup.Show();
                    lblMessage.Text = "Role already exist!";
                }
                else
                {
                    int retval = db.RegisterRole(txtRoleCode.Value, txtRoleDesc.Text.Trim(), ddlRoleType.SelectedValue, is_Enabled, ((UserVO)Session["CurrentUserVO"]).userId);

                    if (retval != 0)
                    {
                        btnClickPopup.Show();
                        lblMessage.Text = "Role added successfully!";
                        ResetFields();
                    }
                    else
                    {
                        btnClickPopup.Show();
                        lblMessage.Text = "Error adding role!";
                    }
                }
            }
            else
            {

                txtRoleCode.Disabled = false;
                dtUserDetails = db.GetRoleDetailsByRoleId(Convert.ToString(txtRoleCode.Value.Trim()), 1);

                if (dtUserDetails.Rows.Count <= 0)
                {
                    btnClickPopup.Show();
                    lblMessage.Text = "Role Code does not exist!";
                }
                else if (dtUserDetails.Rows.Count == 1)
                {
                    int retval = db.ModifyRole(txtRoleCode.Value, txtRoleDesc.Text.Trim(), ddlRoleType.SelectedValue, is_Enabled, ((UserVO)Session["CurrentUserVO"]).userId);

                    if (retval != 0)
                    {
                        lblMessage.Text = "Role modified sucessfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        lblMessage.Text = "Error Modifying role!";
                        btnClickPopup.Show();
                    }
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        private void ResetFields()
        {
            rbOption.SelectedValue = "1";
            bntgetdetails.Enabled = false;
            bntbrowse.Enabled = false;
            txtRoleCode.Value = "";
            ddlRoleType.SelectedValue = "0";
            txtRoleDesc.Text = "";            
            chkEnable.Checked = true;
            txtRoleCode.Disabled = false;
            
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        protected void rbOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rbOption.SelectedValue.Trim() == "1")
            {
                bntbrowse.Enabled = false;
                bntgetdetails.Enabled = false;
                txtRoleCode.Disabled =  false;
            }
            else
            {
                bntbrowse.Enabled = true;
                bntgetdetails.Enabled = true;
                txtRoleCode.Disabled = true;
            }
        }
    }
}